package org.zgy.stock.utils.okhttp3;

import okhttp3.*;

import java.io.IOException;
import java.net.SocketException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: <a href='mailto:a442391947@gmail.com'>Zhanggy</a>
 * @Date 2021/8/7 17:24
 */
public class OkHttpUtil {

    /**
     * 发送Get请求
     * @param url 请求的URL
     * @param queryParam 请求参数
     * @return
     */
    public static String getRequest(String url, Map<String,String> queryParam) {
        OkHttpClient client = new OkHttpClient();

        HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder();
        if (queryParam != null){
            queryParam.entrySet().forEach(item -> {
                httpBuilder.addQueryParameter(item.getKey(),item.getValue());
            });
        }
        Request request = new Request.Builder()
                .url(httpBuilder.build())
//                .addHeader("Host", "push2.eastmoney.com")
//                .addHeader("Referer", "https://quote.eastmoney.com/center/gridlist.html")
//                .addHeader("Cache-Control", "no-cache")
//                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36")
                .build();

        try (Response response = client.newCall(request).execute()) {

            return response.body().string();
        }catch (IOException exception){
            exception.printStackTrace();
            return null;
        }
    }
}
